package com.haiyu.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.WorkerMapper;
import com.haiyu.manager.pojo.Worker;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.WorkerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkerServiceImpl implements WorkerService {
    @Autowired
    WorkerMapper workerMapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public PageDataResult getWorkers(String openId, Integer status, Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<Worker> workers = workerMapper.getWorkers(openId, status);
        System.out.println("workers记录有" + workers);
        PageHelper.startPage(pageNum, pageSize);
        if (workers.size() != 0) {
            PageInfo<Worker> pageInfo = new PageInfo<>(workers);
            pageDataResult.setList(workers);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> updateWorker(Worker worker) {
        Map<String, Object> data = new HashMap();
        try {

            int result = workerMapper.updateWorker(worker);
            if (result == 0) {
                data.put("code", 0);
                data.put("msg", "更新失败！");
                logger.error("worker[更新]，结果=更新失败！");
                return data;
            }
            data.put("code", 1);
            data.put("msg", "更新成功！");
            logger.info("worker[更新]，结果=更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("worker[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> delWorker(Integer id, Integer isAllowed) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除用户
            int result = workerMapper.updateWorkerIsAllowed(id, isAllowed);
            if (result == 0) {
                data.put("code", 0);
                data.put("msg", "删除worker失败");
                logger.error("删除worker失败");
                return data;
            }
            data.put("code", 1);
            data.put("msg", "删除worker成功");
            logger.info("删除worker成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除worker异常！", e);
        }
        return data;
    }

    @Override
    public Map<String, Object> recoverWorker(Integer id, Integer isAllowed) {
        Map<String, Object> data = new HashMap<>();
        try {
            int result = workerMapper.updateWorkerIsAllowed(id, isAllowed);
            if (result == 0) {
                data.put("code", 0);
                data.put("msg", "恢复用户失败");
            }
            data.put("code", 1);
            data.put("msg", "恢复用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("恢复用户异常！", e);
        }
        return data;
    }

    @Override
    public Worker getWorkerById(Integer id) {
        return workerMapper.selectByPrimaryKey(id);
    }
}
